/**
 * 育儿网问答
 */

const puppeteer = require('puppeteer');
const fs = require("fs");

const timestart = new Date().getTime();

(async() => {
	// 默认启动内嵌的 chrome
	const browser = await puppeteer.launch(
		{args: ['--no-sandbox']}
	);
	// open webpage
	const page = await browser.newPage();
	await page.setRequestInterception(true);
	page.on('request', interceptedRequest => {
		const url = interceptedRequest.url().toLowerCase();
		for (name of ['.jpg', '.jpeg', '.png', '.gif']){
			if (url.endsWith(name)){
				// 终止请求
				interceptedRequest.abort();
				return ;
			}
		}
		interceptedRequest.continue();
	});
	// 页数限制
	for (let i=1; i<=10000; i++){
		const url = `http://ask.ci123.com/categories/listquestion/solved?p=${i}`;
		try {
			await page.goto(url, {waitUntil: 'domcontentloaded'});
			const listHdlArr = await page.$x('.//*[@id="list_ask_middle2"]//*[@class="rank_row_left"]');
			if (listHdlArr == null){
				console.log(`p${i} 找不到最新电影列表`);
				continue ;
			}
			// 逐个处理 列表项
			let result = [];
			await (async()=>{
				for (liHdl of listHdlArr){
					// url
					const urlHdl = await liHdl.$x('a/@href');
					const url = await textValue(urlHdl[0], page);
					// 分类
					const catHdl = await liHdl.$x('span/a/text()');
					const category = await textValue(catHdl[0], page);

					result.push({ url, category })
				}
			})();
			// 保存文件
			const savePath = 'dat/ask.ci123.com_url.json'
			fs.writeFile(savePath, JSON.stringify(result)+"\n", {
					encoding: 'utf8',
					mode: 0666,
					flag: 'a'
				}, err=>{
					if (err){
						return console.error(err);

				}
			})
		} catch (e){
			console.log(`p${i}`, e);
		}
	}
	
	// close browser
	await browser.close();
	
	const timeend = new Date().getTime();
	console.log(timestart, timeend, timeend - timestart);


})();

async function textValue(jsHandle){
	const text = await jsHandle.executionContext().evaluate(e=> e.textContent, jsHandle);
	return text;
}
